package zozopo.warehouse;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.comm.ZOZOPOCODE;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.DatabaseManager;
import zozopo.flow.comm.Flow;
import zozopo.flow.comm.FlowCode;
import zozopo.flow.operations.RecordlistTB;

import zozopo.warehouse.operations.AftersaleserviceOS;
import zozopo.warehouse.operations.AftersaleserviceTB;
import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.ReceiptOS;
import zozopo.warehouse.operations.ReceiptTB;
import zozopo.warehouse.operations.ReceiptlistOS;
import zozopo.warehouse.operations.ReceiptlistTB;
import zozopo.warehouse.operations.ReceiptrecordOS;
import zozopo.warehouse.operations.ReceiptrecordTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class CancelReceiptlistAction extends ActionSupport implements Action,SessionAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	                                          
	
	//传入参数
	private int id;
	
	//传出参数
	private String operationResult;
	private ReceiptlistTB receiptlist;
	private List<ReceiptTB> receiptList;
	private List<AftersaleserviceTB> aftersaleserviceList;
	
	
	private Map<Object, Object> session;
	
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		
		///权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
		//用户权限		
		if(operator == null){
			addFieldError("errormessage", "用户未登陆或超时.请登陆后操作.");
			return NONE;
		}

		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = databaseManager.getConnection();
		
		ReceiptlistOS receiptlistOS = new ReceiptlistOS(conn);
		ReceiptrecordOS receiptrecordOS = new ReceiptrecordOS(conn);
		ReceiptOS receiptOS = new ReceiptOS(conn);
		PurchaseOS purchaseOS = new PurchaseOS(conn);
		AftersaleserviceOS aftersaleserviceOS = new AftersaleserviceOS(conn);
		Flow flow = new Flow(conn);
		
		//flow.getFlowValueMapByKind(kind)
		RecordlistTB recordlist = flow.getRecordlist(FlowCode.WAREHOUSE_RECEIPTLIST, id);
		if(recordlist.getNextvalue_recordlist() != 1){  //最初状态 nextvalue = 1 改变后非1
			
			operationResult = "出仓单流程状态已经改变,无法取消.如需取消需要把流程取消或把出仓流程回到最初已打出仓单状态!";
			
			receiptlist = receiptlistOS.getReceiptlistById(id);
			receiptList = receiptOS.getListByReceiptlistId(id);
			aftersaleserviceList = aftersaleserviceOS.selectByReceiptlistid(id);
			databaseManager.releaseConnection(conn);
			return SUCCESS;
		}
		
		try {
			
			
			
			conn.setAutoCommit(false);
			
			ReceiptlistTB receiptlist = receiptlistOS.getReceiptlistById(id); 
			
			
			String state = receiptlist.getState_receiptlist();
			
			if(!state.equals(ZOZOPOCODE.NORMAL)){
				//不是默认状态不能取稍.
				//databaseManager.releaseConnection(conn);
				throw new Exception();
			}
			
			if(!operator.getEmail_operator().equals(receiptlist.getUseremail_receiptlist())){
				//databaseManager.releaseConnection(conn);
				throw new Exception();
			}
			
			
			
			
			List<ReceiptTB> receiptList = receiptOS.getListByReceiptlistId(id);
			
			
			List<ReceiptrecordTB> receiptrecordlist = receiptrecordOS.getList(receiptList); //获得出仓记录.
			
			if(!receiptlistOS.cancel(id)){
				throw new SQLException();
			}
			
			if(!receiptOS.cancel(receiptList)){
				throw new SQLException();
			} 
			if(!purchaseOS.cancel(receiptrecordlist)){
				throw new SQLException();
			}
			
			//receiptrecordOS.   未加入取消记录
			 
			
			conn.commit();
			conn.setAutoCommit(true);
			operationResult = "id"+id + "已经成功被取消";
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			operationResult = "数据库错误";
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}catch (Exception e) 
		{
			operationResult = "系统错误";
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			receiptlist = receiptlistOS.getReceiptlistById(id);
			receiptList = receiptOS.getListByReceiptlistId(id);
			aftersaleserviceList = aftersaleserviceOS.selectByReceiptlistid(id);
			databaseManager.releaseConnection(conn);
		}

		
		return SUCCESS;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getOperationResult() {
		return operationResult;
	}
	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}
	public ReceiptlistTB getReceiptlist() {
		return receiptlist;
	}
	public void setReceiptlist(ReceiptlistTB receiptlist) {
		this.receiptlist = receiptlist;
	}
	public List<ReceiptTB> getReceiptList() {
		return receiptList;
	}
	public void setReceiptList(List<ReceiptTB> receiptList) {
		this.receiptList = receiptList;
	}
	public List<AftersaleserviceTB> getAftersaleserviceList() {
		return aftersaleserviceList;
	}
	public void setAftersaleserviceList(
			List<AftersaleserviceTB> aftersaleserviceList) {
		this.aftersaleserviceList = aftersaleserviceList;
	}

}
